<?php
/**
 * 物流自提服务站首页
 *
 * * @网店运维 (c) 2015-2018 ShopWWI Inc. (http://www.shopwwi.com)
 * @license    http://www.shopwwi.c om
 * @link       交流群号：111731672
 * @since      网店运维提供技术支持 授权请购买shopnc授权
 */
defined('ByShopWWI') or exit('Access Invalid!');
class optionsControl extends BaseChainCenterControl
{
    public function __construct()
    {
        parent::__construct();
        // $model_member = Model('member');
        $sj = Model('store')->getStoreInfoByID($_SESSION['chain_store_id']);
        $model_member = Model('member');
        $member_info = $model_member->getMemberInfo(array('member_id' => $sj['member_id']));
        Tpl::output('member_info', $member_info);
        // $model_member = Model('member');
        // $member_info = $model_member->getMemberInfo(array('member_id'=>$_SESSION['chain_id']));
        // exit(print_r($member_info));
    }
    public function indexOp()
    {
        $model_order = Model('order');
        $condition = array();
        $condition['chain_id'] = $_SESSION['chain_name'];
        $condition['chain_code'] = array('gt', 0);
        // Tpl::output('options_list',$order_list);
        Tpl::output('show_page', $model_order->showpage());
        Tpl::showpage('options.list');
    }
    public function checkmemberOp()
    {
        $name = trim($_GET['name']);
        if (!$name) {
            echo '';
            die;
        }
        /**
         * 转码
         */
        if (strtoupper(CHARSET) == 'GBK') {
            $name = Language::getGBK($name);
        }
        $obj_member = Model('member');
        $member_info = $obj_member->getMemberInfo(array('member_name' => $name));
        if (is_array($member_info) && count($member_info) > 0) {
            if (strtoupper(CHARSET) == 'GBK') {
                $member_info['member_name'] = Language::getUTF8($member_info['member_name']);
            }
            $member_info['member_mobile'] = Language::getUTF8($member_info['member_mobile']);
            echo json_encode(array('id' => $member_info['member_id'], 'name' => $member_info['member_name'],'member_truename' => $member_info['member_truename'], 'mobile' => $member_info['member_mobile'], 'points' => round($member_info['member_points'], 2)));
        } else {
            echo '';
            die;
        }
    }
    public function shoukuanOp()
    {
       
        $chain_id = $_SESSION['chain_id'];//门店ID
		
		$chain_user = $_SESSION['chain_user'];//门店账号
		
		$chain_name = $_SESSION['chain_name'];//门店名称
		
		$chain_store_id = $_SESSION['chain_store_id'];//商家店铺id
		
		$store_id = $chain_store_id;//转换短的店铺id
				
        $member_id = $_POST['member_id'];//获取的消费会员账号id
        
		$member_name = $_POST['member_name'];//获取的消费会员账号名称
        $member_sm = $_POST['member_shuming'];//获取的消费备注信息
		
        $member_je = $_POST['member_amount'];//获取的消费金额
		
        $pamount = $member_je;//定义pamount为消费金额
		
        $member_points = $member_je * 100;//计算消费金额兑换宝豆
        //$xf_date = strtotime(date("Y-m-d H:i:s", time()));
		
       
        if ($member_id>0) {
          
		   //定义操作类型
         $model_member = Model('member');
		
		 //获取消费会员信息
         //$member_list = $model_member->where(array('member_id' => $member_id))->Select();
         //$member_id = $member_list[0]['member_id'];
		 //$invite_one = $member_list[0]['invite_one'];
         //$invite_two = $member_list[0]['invite_two'];
         //$invite_three = $member_list[0]['invite_three'];
         ////$member_name = $member_list[0]['member_name'];
         //$vip = $member_list[0]['member_exppoints'];
		
		 $member_info_xf = $model_member->getMemberInfo(array('member_id' => $member_id));
		
         //$member_id = $member_info_xf['member_id'];
		
		 $invite_one = $member_info_xf['invite_one'];
		
         $invite_two = $member_info_xf['invite_two'];
		
         $invite_three = $member_info_xf['invite_three'];
		
         //$member_name = $member_info_xf['member_name'];
		
		 //获取会员经验值
         $vip = $member_info_xf['member_exppoints'];
		
		
		 //通过商家店铺id获取商家会员账号id信息
		 $sjinfo = Model('store')->getStoreInfoByID($store_id);
		 $sj_store_membername=$sjinfo['member_name']; 
		 //$sj_store_name=$sjinfo['member_name']; 

		
		 //获取商家会员账号信息
         $sj_member_info = $model_member->getMemberInfo(array('member_id' => $sjinfo['member_id']));
         $sj_invite_one = $sj_member_info['invite_one'];
         $sj_invite_two = $sj_member_info['invite_two'];
         $sj_invite_three = $sj_member_info['invite_three'];
         $sj_member_name = $sj_member_info['member_name'];
		
		 $sj_member_id = $sj_member_info['member_id'];
		
		 $sj_kcpoints = $sj_member_info['kucunpoints'];
        
		 //商家会员账号推荐代理人id
		 $agent_store_rec_id = $sj_member_info['agent_store_rec_id'];
		
		 $pl_addtime = strtotime(date("Y-m-d H:i:s"));
		
		
		 /////////////////////////////////
		 //查询门店所属的会员账号信息（此处有疑问，应该回去会员表的member_name信息，不应该获取store表的member_name）
		 //$md_store = Model('store');
		 //$md_store_list = $md_store->where(array('store_id' => $store_id))->Select();
		 //$me_name= $md_store_list[0]['member_name'];//自身会员号
		
		 //判断是否是自己购买
		    if($sj_member_id==$member_id){
			 showDialog('不能购买自己店铺的产品!');
			 exit();
			}
            //////////////////////////////
		
            // 添加门店消费记录
            $sql_xf = "insert into shopwwi_xiaofei(member_id,chain_id,store_id,member_name,pamount_num,pamount_sm,pamount_date) values({$member_id},{$chain_id},{$store_id},'{$member_name}',{$pamount},'{$pamount_sm}',{$pl_addtime})";
            Model()->execute($sql_xf);
            // 增加会员消费豆
            Model()->execute('update shopwwi_member set member_points = member_points + ' . $member_points . ' where member_id=' . $member_id);
            //添加会员消费豆日志
			//pl_stage='consume'
            $sql_bc = "insert into shopwwi_points_log(pl_memberid,pl_membername,pl_points,pl_addtime,pl_desc,pl_stage) values({$member_id},'{$member_name}',{$member_points},{$pl_addtime},'[" . $member_name . "]在[" . $sj_store_membername . "]的[" . $chain_name . "]门店消费" . $pamount . "元，宝豆返还-宝豆增加','storepoints')";
            Model()->execute($sql_bc);
            //修改商家库存积分
            $m_points = "-" . $member_points;
            Model()->execute('update shopwwi_member set kucun_points = kucun_points - ' . $member_points . ' where member_id=' . $sj_member_id);
            //添加商家库存积分日志
			//pl_stage='kucunpointssell'
            $sql_bc = "insert into shopwwi_points_log(pl_memberid,pl_membername,pl_points,pl_addtime,pl_desc,pl_stage) values({$sj_member_id},'{$sj_member_name}',{$m_points},{$pl_addtime},'[" . $member_name . "]在[" . $sj_store_membername . "]的[" . $chain_name . "]门店消费" . $pamount . "元，商家" . $sj_member_name . "的库存积分使用-库存积分减少','kucunpoints')";
            Model()->execute($sql_bc);
            //商家的会员账号返16%
            $shiliu = $member_points * 16 / 100;
			
            // 增加商家16%的返还库存使用积分			
            Model()->execute('update shopwwi_member set member_points = member_points + ' . $shiliu . ' where member_id=' . $sj_member_id);
            
			// 增加商家16%的返还库存使用积分日志\
			//pl_stage='storeback'
            $sql_bc = "insert into shopwwi_points_log(pl_memberid,pl_membername,pl_points,pl_addtime,pl_desc,pl_stage) values({$sj_member_id},'{$sj_member_name}',{$shiliu},{$pl_addtime},'[" . $member_name . "]在[" . $sj_store_membername . "]的[" . $chain_name . "]门店消费" . $pamount . "元，商家" . $sj_member_name . "的库存积分使用-商家16%的宝豆返还-宝豆增加','kucunpoints')";
            Model()->execute($sql_bc);
			
              if ($invite_one > 0) {
               //获取消费会员一级推荐人信息
                $member_invite_info = $model_member->getMemberInfo(array('member_id' => $invite_one));
				
				$invite_member_name = $member_invite_info['member_name'];
                $invite_member_id = $member_invite_info['member_id'];
				
                //获取消费会员一级推荐人等级返现参数
                $member_gradeinfo = $model_member->getOneMemberGrade(intval($member_invite_info['member_exppoints']));
                
				//计算返佣分红豆pamount=会员消费金额
                $fy_huipoints = $pamount * $member_gradeinfo['nxmoney'];
				//消费会员一级推荐会员分红豆增加
                Model()->execute('update shopwwi_member set member_red_points = member_red_points + ' . $fy_huipoints . ' where member_id=' . $invite_member_id);
                //消费会员一级推荐会员分红豆日志
                //pl_stage='invitepoints'
                $sql_bc = "insert into shopwwi_points_log (pl_memberid,pl_membername,pl_points,pl_addtime,pl_desc,pl_stage,pl_upmemberid) values({$invite_member_id},'{$invite_member_name}',{$fy_huipoints},{$pl_addtime},'[" . $member_name . "]在[" . $sj_store_membername . "]的[" . $chain_name . "]门店消费" . $pamount . "元，上级推荐会员佣金提成-分红豆增加','redpoints','". $member_id ."' )";
				Model()->execute($sql_bc);
              }
              
			  if ($invite_two > 0) {
                //获取消费会员二级推荐人信息
                $member_invite_info = $model_member->getMemberInfo(array('member_id' => $invite_two));
				
				$invite_member_name = $member_invite_info['member_name'];
                $invite_member_id = $member_invite_info['member_id'];
				//获取消费会员一级推荐人等级返现参数
                $member_gradeinfo = $model_member->getOneMemberGrade(intval($member_invite_info['member_exppoints']));
				//计算返佣分红豆pamount=会员消费金额
                $fy_huipoints = $pamount * $member_gradeinfo['nnxmoney'];
                //消费会员二级推荐会员分红豆增加
				 Model()->execute('update shopwwi_member set member_red_points = member_red_points + ' . $fy_huipoints . ' where member_id=' . $invite_member_id);
                //消费会员二级推荐会员分红豆日志增加
                //pl_stage='invitepoints'
                $sql_bc = "insert into shopwwi_points_log (pl_memberid,pl_membername,pl_points,pl_addtime,pl_desc,pl_stage,pl_upmemberid) values({$invite_member_id},'{$invite_member_name}',{$fy_huipoints},{$pl_addtime},'[" . $member_name . "]在[" . $sj_store_membername . "]的[" . $chain_name . "]门店消费" . $pamount . "元，上上级推荐会员佣金提成-分红豆增加','redpoints','". $member_id ."' )";
				Model()->execute($sql_bc);
               }
                //以下是商家
			//一级推荐分红豆提成
                if ($sj_invite_one > 0) {
                    $member_invite_info = $model_member->getMemberInfo(array('member_id' => $sj_invite_one));
					
                    $invite_member_name = $member_invite_info['member_name'];
                    $invite_member_id = $member_invite_info['member_id'];
					
                    $member_gradeinfo = $model_member->getOneMemberGrade(intval($member_invite_info['member_exppoints']));
                    $sj_huipoints = $pamount * $member_gradeinfo['sale_tc'];
                   
				   //分红豆
				     Model()->execute('update shopwwi_member set member_red_points = member_red_points + ' . $sj_huipoints . ' where member_id=' . $invite_member_id);
                    //分红豆日志
                //pl_stage='invitepoints'
                    $sql_bc = "insert into shopwwi_points_log (pl_memberid,pl_membername,pl_points,pl_addtime,pl_desc,pl_stage,pl_upmemberid) values({$invite_member_id},'{$invite_member_name}',{$sj_huipoints},{$pl_addtime},'[" . $member_name . "]在[" . $sj_store_membername . "]的[" . $chain_name . "]门店消费" . $pamount . "元，" . $sj_member_name . "商家的上级推荐会佣金分提成-分红豆增加','redpoints','". $member_id ."')";
                    Model()->execute($sql_bc);
                }
                 //商家二级推荐分红豆提成
                if ($sj_invite_two > 0) {
                    $member_invite_info = $model_member->getMemberInfo(array('member_id' => $sj_invite_two));
                    $invite_member_name = $member_invite_info['member_name'];
                    $invite_member_id = $member_invite_info['member_id'];
					
					$member_gradeinfo = $model_member->getOneMemberGrade(intval($member_invite_info['member_exppoints']));
                    //商家推荐会员提成（分红豆）
                    $sj_huipoints = $pamount * $member_gradeinfo['nsale_tc'];
                    //商家二级推荐分红豆提成
					Model()->execute('update shopwwi_member set member_red_points = member_red_points + ' . $sj_huipoints . ' where member_id=' . $invite_member_id);
                    //商家二级推荐分红豆提成日志
                //pl_stage='invitepoints'
                    $sql_bc = "insert into shopwwi_points_log (pl_memberid,pl_membername,pl_points,pl_addtime,pl_desc,pl_stage,pl_upmemberid) values({$invite_member_id},'{$invite_member_name}',{$sj_huipoints},{$pl_addtime},'[" . $member_name . "]在[" . $sj_store_membername . "]的[" . $chain_name . "]门店消费" . $pamount . "元，" . $sj_member_name . "商家的上上级推荐会员佣金提成-分红豆增加','redpoints','". $member_id ."')";
                    Model()->execute($sql_bc);
                }
				 
				 //以下是代理商的提成
            if ($agent_store_rec_id > 0) {
				//获取代理商信息
                $member_invite_info = $model_member->getMemberInfo(array('member_id' => $agent_store_rec_id));
                $agent_member_name = $member_invite_info['member_name'];
                $agent_member_id = $member_invite_info['member_id'];
				$agent_member_exppoints = $member_invite_info['member_exppoints'];
				if ($agent_member_exppoints = 20999 ) {
			    //代理商提成参数
			    $agent_storetc=0.5;
				
				//$member_gradeinfo = $model_member->getOneMemberGrade(intval($member_invite_info['member_exppoints']));
                //计算代理商提成（分红豆）
			    //$fy_hui_points = $pamount * $member_gradeinfo['sale_tc'];
				$fy_hui_points = $pamount * $agent_storetc;
				//代理商分红豆增加
				Model()->execute('update shopwwi_member set member_red_points = member_red_points + ' . $fy_hui_points . ' where member_id=' . $agent_member_id);
                //代理商分红豆日志
                //pl_stage='invitepoints'
                $sql_bc = "insert into shopwwi_points_log (pl_memberid,pl_membername,pl_points,pl_addtime,pl_desc,pl_stage,pl_upmemberid) values({$agent_member_id},'{$agent_member_name}',{$fy_hui_points},{$pl_addtime},'[" . $member_name . "]在[" . $sj_store_membername . "]的[" . $chain_name . "]门店消费" . $pamount . "元，" . $sj_member_name . "的推荐代理商佣金提成-分红豆增加','redpoints','". $member_id ."')";
                Model()->execute($sql_bc);
				}
            }
            echo "<script>alert('收银成功!');location.href='/chain/index.php?act=options'</script>";
            // ********************************************
        } else {
            echo "<script>alert('账号不存在');history.go(-1)</script>";
        }
    }
    /**
     * 提货验证
     */
    public function pickup_parcelOp()
    {
        if (chksubmit()) {
            $order_id = intval($_POST['order_id']);
            $pickup_code = intval($_POST['pickup_code']);
            if ($order_id <= 0 || $pickup_code <= 0) {
                showDialog(L('wrong_argument'));
            }
            $model_order = Model('order');
            $order_info = $model_order->getOrderInfo(array('order_id' => $order_id, 'chain_code' => $pickup_code));
            if (empty($order_info)) {
                showDialog('提货码错误');
            }
            $logic_order = Logic('order');
            $if_allow = $model_order->getOrderOperateState('chain_receive', $order_info);
            if (!$if_allow) {
                showDialog('无权操作');
            }
            $result = $logic_order->changeOrderStateReceive($order_info, 'chain', '自提门店', '商品已被买家自提，门店更改订单为完成状态');
            if ($result['state']) {
                showDialog('提货成功', 'reload', 'succ', 'DialogManager.close("pickup_parcel")');
            } else {
                showDialog($result['msg']);
            }
        } else {
            $order_model = Model('order');
            $condition['order_id'] = intval($_GET['order_id']);
            $condition['chain_id'] = $_SESSION['chain_id'];
            $order_info = $order_model->getOrderInfo($condition, array('order_common', 'order_goods'));
            foreach ($order_info['extend_order_goods'] as $k => $value) {
                $value['image_url'] = cthumb($value['goods_image'], 60, $value['store_id']);
                $value['goods_type_cn'] = orderGoodsType($value['goods_type']);
                $value['goods_url'] = urlShop('goods', 'index', array('goods_id' => $value['goods_id']));
                $order_info['extend_order_goods'][$k] = $value;
            }
            usort($order_info['extend_order_goods'], function ($a, $b) {
                if ($a['goods_type'] == $b['goods_type']) {
                    return 0;
                }
                return $a['goods_type'] > $b['goods_type'] ? 1 : -1;
            });
            Tpl::output('order_info', $order_info);
        }
        Tpl::showpage('order.pickup_parcel', 'null_layout');
    }
}